/*
 * slider_test.c
 *
 *  Created on: 2020年7月25日
 *      Author: AN
 */
#include <lv_examples/terminal/lemter_test.h>
#include "lvgl.h"
#include <stdio.h>

static lv_obj_t * slider_label;
static lv_obj_t * btn_label;
lv_obj_t *gauge1,*gauge2,*gauge3,*gauge4;
//lv_obj_t * cont_1;
lv_obj_t * page_1;


void lvDel_tabView();


void lv_obj_set_gaval(lv_obj_t * obj, lv_coord_t gaval)
{
    lv_gauge_set_value(obj, 0, gaval);
}

static void cb_sliderTest(lv_obj_t * slider, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */
        snprintf(buf, 4, "%u", lv_slider_get_value(slider));
        lv_label_set_text(slider_label, buf);
		
//		lv_slider_set_value(slider, 10, LV_ANIM_ON);
        /* INITIALIZE AN ANIMATION
        *-----------------------*/
//        lv_anim_t a;
//        lv_anim_init(&a);
//        /* MANDATORY SETTINGS
//        *------------------*/
//        /*Set the "animator" function*/
//        lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t) lv_obj_set_gaval);
//        /*Set the "animator" function*/
//        lv_anim_set_var(&a, gauge1);
//        /*Length of the animation [ms]*/
//        lv_anim_set_time(&a, 1000);
//        /*Set start and end values. E.g. 0, 150*/
//        lv_anim_set_values(&a, lv_gauge_get_value(gauge1, 0), lv_slider_get_value(slider)*2);
//        lv_anim_start(&a);
    }
}


void btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    static uint8_t t=false;
    if(event == LV_EVENT_CLICKED) {
        if(t==false){
            lv_label_set_text(btn_label, "click");
//            lvDel_tabView();
            lv_obj_set_hidden(page_1, true);
        }
        if(t==true){
            lv_label_set_text(btn_label, "Button");
//            lvAdd_tabView();
            lv_obj_set_hidden(page_1, false);
        }
        t=!t;
//        lv_gauge_set_value(gauge1, 0, 0);

    }
}

void lvAdd_tabView(void)
{
//	lv_obj_t *tabview;
//	tabview = lv_tabview_create(lv_scr_act(), NULL);
//	/*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
//	lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Tab 1");
//	lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Tab 2");
//	lv_obj_t *tab3 = lv_tabview_add_tab(tabview, "Tab 3");


    page_1 = lv_page_create(lv_scr_act(), NULL);
    lv_obj_set_size(page_1, 480, 200);
    lv_obj_align(page_1, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
//    lv_page_set_scrl_height(page_1, 300);
    lv_page_set_scrollbar_mode(page_1, LV_SCROLLBAR_MODE_ON);

    lv_obj_t * slider = lv_slider_create(page_1, NULL);
    lv_obj_set_width(slider, LV_DPI * 2);
    lv_obj_align(slider, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 10);
    lv_obj_set_event_cb(slider, cb_sliderTest);
    lv_slider_set_range(slider, 0, 100);
    // Create a label below the slider
    slider_label = lv_label_create(page_1, NULL);
    lv_label_set_text(slider_label, "0");
    lv_obj_set_auto_realign(slider_label, true);
    lv_obj_align(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);

    //Describe the color for the needles
    static lv_color_t needle_colors[3];
    needle_colors[0] = LV_COLOR_BLUE;
    needle_colors[1] = LV_COLOR_ORANGE;
    needle_colors[2] = LV_COLOR_PURPLE;
    //Create a gauge
    gauge1 = lv_gauge_create(page_1, NULL);
    lv_gauge_set_range(gauge1,0,200);
    lv_gauge_set_critical_value(gauge1, 110);
    lv_gauge_set_scale(gauge1, 300, 21, 5);
    lv_gauge_set_needle_count(gauge1, 1, needle_colors);
    lv_obj_set_size(gauge1, 150, 150);
    lv_obj_align(gauge1, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_obj_set_click(gauge1, false);
    //Set the values
    lv_gauge_set_value(gauge1, 0, 0);

    gauge2 = lv_gauge_create(page_1, NULL);
    lv_gauge_set_range(gauge2,0,200);
    lv_gauge_set_critical_value(gauge2, 110);
    lv_gauge_set_scale(gauge2, 300, 21, 5);
    lv_gauge_set_needle_count(gauge2, 1, needle_colors);
    lv_obj_set_size(gauge2, 150, 150);
    lv_obj_align(gauge2, NULL, LV_ALIGN_IN_TOP_LEFT, 155, 0);
    lv_obj_set_click(gauge2, false);
    //Set the values
    lv_gauge_set_value(gauge2, 0, 0);


    gauge3 = lv_gauge_create(page_1, NULL);
    lv_gauge_set_range(gauge3,0,200);
    lv_gauge_set_critical_value(gauge3, 110);
    lv_gauge_set_scale(gauge3, 300, 21, 5);
    lv_gauge_set_needle_count(gauge3, 1, needle_colors);
    lv_obj_set_size(gauge3, 150, 150);
    lv_obj_align(gauge3, NULL, LV_ALIGN_IN_TOP_LEFT, 310, 0);
    lv_obj_set_click(gauge3, false);
    //Set the values
    lv_gauge_set_value(gauge3, 0, 0);

    gauge4 = lv_gauge_create(page_1, NULL);
    lv_gauge_set_range(gauge4,0,200);
    lv_gauge_set_critical_value(gauge4, 110);
    lv_gauge_set_scale(gauge4, 300, 21, 5);
    lv_gauge_set_needle_count(gauge4, 1, needle_colors);
    lv_obj_set_size(gauge4, 150, 150);
    lv_obj_align(gauge4, NULL, LV_ALIGN_IN_TOP_LEFT, 465, 0);
    lv_obj_set_click(gauge4, false);

    //Set the values
    lv_gauge_set_value(gauge4, 0, 0);
}


void lvDel_tabView()
{
    lv_page_clean(page_1);
//    lv_obj_clean(page_1);
//    lv_obj_clean(slider_label);
//    lv_obj_clean(gauge1);
//    lv_obj_clean(gauge2);
//    lv_obj_clean(gauge3);
}

//例程入口
lv_obj_t * slider;
void lvAdd_sliderTest()
{
    slider = lv_slider_create(lv_scr_act(), NULL);
    lv_obj_set_width(slider, LV_DPI*1.5);
    lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(slider, cb_sliderTest);
    lv_slider_set_range(slider, 0, 50);
    // Create a label below the slider
    slider_label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(slider_label, "0");
    lv_obj_set_auto_realign(slider_label, true);
    lv_obj_align(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
}

void lvChange_sliderTest()
{
	lv_slider_set_value(slider, 10, LV_ANIM_ON);
//	static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */
//	snprintf(buf, 4, "%u", lv_slider_get_value(slider));
//	lv_label_set_text(slider_label, buf);
//	/* INITIALIZE AN ANIMATION
//	*-----------------------*/
//	lv_anim_t a;
//	lv_anim_init(&a);
//	/* MANDATORY SETTINGS
//	*------------------*/
//	/*Set the "animator" function*/
//	lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t) lv_obj_set_gaval);
//	/*Set the "animator" function*/
//	lv_anim_set_var(&a, gauge1);
//	/*Length of the animation [ms]*/
//	lv_anim_set_time(&a, 1000);
//	/*Set start and end values. E.g. 0, 150*/
//	lv_anim_set_values(&a, lv_gauge_get_value(gauge1, 0), lv_slider_get_value(slider)*2);
//	lv_anim_start(&a);
}
